package com.jlastu.dlxy_androidapp.view.sell;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.jlastu.dlxy_androidapp.R;
import com.jlastu.dlxy_androidapp.contract.StoreManageContract;
import com.jlastu.dlxy_androidapp.entity.Store;
import com.jlastu.dlxy_androidapp.presenter.StoreManagePresenter;
import com.jlastu.dlxy_androidapp.utils.SpUtils;

public class StoreManageActivity extends AppCompatActivity implements StoreManageContract.IStoreManageView {

    private SpUtils mSpUtils;
    private StoreManageContract.IStoreManagePresenter mStoreManagePresenter;
    private TextView mText1;
    private TextView mText2;
    private int user_id;
    private Store mStore;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopmanagement);
        mStoreManagePresenter = new StoreManagePresenter(this,this);
        mText1 = findViewById(R.id.text1);
        mText2 = findViewById(R.id.text2);
        mSpUtils = new SpUtils(getApplicationContext());
        user_id = mSpUtils.getInt("userid",0);
        mText1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String fieldName = "店铺名称";
                showInputDialog(mText1, fieldName);
            }
        });
        mText2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String fieldName = "地址";
                showInputDialog(mText2, fieldName);
            }
        });
    }
@Override
public void onResume() {
    super.onResume();
    mStoreManagePresenter.getStore(user_id);
}
    private void showInputDialog(final TextView textView, final String fieldName) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("输入新的" + fieldName);

        final EditText input = new EditText(this);
        input.setInputType(InputType.TYPE_CLASS_TEXT);
        input.setText(textView.getText().toString());
        builder.setView(input);

        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                textView.setText(input.getText().toString());
                updateUser();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

        builder.show();
    }
    private void updateUser() {
        //调用更改方法
        mStore.setStore_name(mText1.getText().toString());
        mStore.setStore_address(mText2.getText().toString());
        mStoreManagePresenter.updateUser(user_id, mStore);

    }

    @Override
    public void showStore(Store store) {
        this.mStore = store;
        mText1.setText(store.getStore_name());
        mText2.setText(store.getStore_address());
    }
}
